<template>
	<view>
		<u-sticky bgColor="#6492FF" offsetTop=0 customNavHeight=0>
			<navbox title="意见反馈"></navbox>
		</u-sticky>
		<view class="con">
			<view class="con__item">
				<view class="con__item__label">
					<text style="color: red;">*</text>反馈类型
				</view>
				<view class="u-flex u-flex-y-center">
					<view class="con__item__type" v-for="(item,index) in typelist" @click="cur=index" :key="index" :class="cur===index?'active':''">
						{{item.dictLabel}}
					</view>
				</view>
			</view>
			<view class="con__item bb0">
				<view class="con__item__label">
					<text style="color: red;">*</text>备注/描述
				</view>
				<u--textarea v-model="form.info" placeholder="请输入备注/描述"></u--textarea>
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:maxCount="10"></u-upload>
			<view class="submit">
				<u-button text="提交" :loading='loading' shape='circle' color="#6492FF" @click="submit"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {advicetypeList,advice} from '@/api/user.js'
	export default {
		data() {
			return {
				form: {
					imgs:'',
					info:'',
					type:''
				},
				fileList1:[],
				upl:true,
				cur:'',
				typelist:[],
				loading:false
			};
		},
		methods: {
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let _this=this
				this.upl=false
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: _this.$BASE_URL+'/app/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							Authorization: uni.getStorageSync('ytbtoken')
						},
						success: (res) => {
							setTimeout(() => {
								
								
								// console.log(JSON.parse(res.data))
								if (JSON.parse(res.data).code == 200) {
									resolve(res.data)
									_this.upl=true
								}else{
									uni.reLaunch({
										url:'/pages/index/login'
									})
								}
							}, 1000)
						}
					});
				})
			},
			submit(){
				if(!this.upl){
					this.$u.toast('图片正在上传')
					return
				}
				let img=[]
				this.fileList1.map(t=>{
					img.push(JSON.parse(t.url).data.url)
				})
				this.form.imgs=img.join(',')
				let {type,info} = this.form
				if(this.cur===''){
					this.$u.toast('请选择类型')
					return
				}
				this.form.type=this.typelist[this.cur].dictCode
				if(!info){
					this.$u.toast('请输入备注/描述')
					return
				}
				this.loading=true
				advice(this.form).then(res=>{
					this.loading=false
					if(res.data.code==200){
						uni.navigateBack()
					}
				})
			}
		},
		onLoad() {
			advicetypeList().then(res=>{
				if(res.data.code==200){
					this.typelist=res.data.data
				}
			})
		}
	}
</script>

<style lang="scss">
	.con {
		padding: 25rpx 40rpx;

		&__item {
			border-bottom: 1rpx solid #e2e2e2;
			padding-bottom: 20rpx;
			margin-bottom: 20rpx;

			&__label {
				margin-bottom: 20rpx;
			}

			&__type {
				padding: 0 20rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				color: #333;
				border-radius: 30rpx;
				border: 2rpx solid #f6f6f6;
				background-color: #f6f6f6;
				font-size: 30rpx;
				margin-right: 30rpx;
			}
			.active{
				color: #6492ff;
				border-color: #6492ff
			}
		}
	}

	.bb0 {
		border-bottom: 0;
	}
	.submit{
		padding: 20rpx 60rpx;
	}
	.u-button {
		height: 60rpx;
	}
</style>